﻿Imports Wyvern.Translation

Class MainWindow

  Private Braillizer As Translator

  Private OpenFile As Microsoft.Win32.OpenFileDialog
  Private SaveFile As Microsoft.Win32.SaveFileDialog

  Public Sub New()
    'This call is required by the designer.
    InitializeComponent()

    'Instantiates a Braille translator.
    Const FilePath As String = "languages.xml"
    Dim Languages As New LanguageConfiguration
    Languages.Add("English", {"Grade 2"})
    Languages.Add("Thai")
    Try
      Braillizer = New Translator(FilePath, Languages)
    Catch ex As Exception
      MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButton.OK, MessageBoxImage.Error)
      End
    End Try

    'Instantiates an open file dialog.
    OpenFile = New Microsoft.Win32.OpenFileDialog
    OpenFile.Filter = "Text documents (.txt)|*.txt"

    'Instantiates a save file dialog.
    SaveFile = New Microsoft.Win32.SaveFileDialog
    SaveFile.Filter = "Text documents (.txt)|*.txt"
  End Sub

  Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
    'Binds mouse hover events to each menu button.
    For i As Integer = 0 To ToolView.Children.Count - 1
      With CType(ToolView.Children(i), Grid)
        AddHandler .MouseEnter, AddressOf Button_MouseEnter
        AddHandler .MouseLeave, AddressOf Button_MouseLeave
      End With
    Next
  End Sub

  Private Sub Button_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
    Dim Target As Grid = sender
    With CType(Target.Children(0), Rectangle)
      .Fill = Brushes.Azure
      .Stroke = Brushes.LightSkyBlue
    End With
  End Sub

  Private Sub Button_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
    Dim Target As Grid = sender
    With CType(Target.Children(0), Rectangle)
      .Fill = Brushes.White
      .Stroke = Brushes.DarkGray
    End With
  End Sub

  Private Sub OpenButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles OpenButton.MouseUp
    'Shows an open file dialog.
    If OpenFile.ShowDialog Then
      HBox.Clear()

      Dim Reader As New System.IO.StreamReader(OpenFile.FileName, True)
      HBox.Text = Reader.ReadToEnd
      Reader.Close()

      Me.Title = "Wyvern - " & OpenFile.SafeFileName
    End If
  End Sub

  Private Sub TranslateButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles TranslateButton.MouseUp
    'Forces the translation.
    HBox_TextChanged(sender, Nothing)
  End Sub

  Private Sub HBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles HBox.TextChanged
    'Sets a character counter.
    CharNum.Text = "C" & HBox.GetLineLength(HBox.GetLineIndexFromCharacterIndex(HBox.SelectionStart))
    If Integer.Parse(CharNum.Text.Substring(1)) > 40 Then
      CharNum.Foreground = Brushes.Red
    Else
      CharNum.Foreground = Brushes.White
    End If

    'Sets a line counter.
    LineNum.Text = "L" & HBox.GetLineIndexFromCharacterIndex(HBox.SelectionStart) + 1

    'Checks whether the HBox is not empty.
    If HBox.Text.Length > 0 Then
      Try
        'Invokes the translation.
        BBox.Text = Braillizer.Translate(HBox.Text)
      Catch ex As Exception
        'Do nothing.
      End Try
    Else
      BBox.Clear()
    End If
  End Sub

  Private Sub SaveButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles SaveButton.MouseUp
    'Shows a save file dialog.
    If SaveFile.ShowDialog Then
      Dim Writer As New System.IO.StreamWriter(SaveFile.FileName, False, System.Text.Encoding.UTF8)
      Writer.Write(HBox.Text)
      Writer.Flush()
      Writer.Close()

      Me.Title = "Wyvern - " & OpenFile.SafeFileName
    End If
  End Sub

  Private Sub CopyButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles CopyButton.MouseUp
    'Copies selected text to the clipboard.
    If HBox.IsFocused Then
      System.Windows.Clipboard.SetText(HBox.Text.Substring(HBox.SelectionStart, HBox.SelectionLength))
    ElseIf BBox.IsFocused Then
      System.Windows.Clipboard.SetText(BBox.Text.Substring(BBox.SelectionStart, BBox.SelectionLength))
    End If
  End Sub

  Private Sub PasteButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles PasteButton.MouseUp
    'Inserts copied text to the HBox.
    Dim IBeam As Integer = HBox.SelectionStart
    HBox.Text = HBox.Text.Insert(HBox.SelectionStart, System.Windows.Clipboard.GetText)
    HBox.SelectionStart = IBeam + System.Windows.Clipboard.GetText.Length
  End Sub

  Private Sub PrintButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles PrintButton.MouseUp
    'Shows message box.
    Select Case MessageBox.Show("You are about to print the current Braille code to the Braille embosser via a line print terminal 1 (LPT1)." & vbNewLine &
                                "Do you want to continue?" & vbNewLine & vbNewLine &
                                "Click Yes to start print using LPT1 parallel port." & vbNewLine &
                                "Click No to start print using a default printer (any port).",
                                "Select a printer", MessageBoxButton.YesNoCancel, MessageBoxImage.Question)
      Case MessageBoxResult.Yes
        'Print plain Braille code to the line print terminal 1 (LPT1).
        Dim Writer As New System.IO.StreamWriter("temp.txt", False, System.Text.Encoding.ASCII)
        Writer.Write(BBox.Text)
        Writer.Close()
        Microsoft.VisualBasic.Shell("cmd /C copy /Y temp.txt lpt1", AppWinStyle.Hide)
        Exit Select

      Case MessageBoxResult.No
        'Print plain Braille code to the default printer (any port).
        Dim Printer As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer()
        Dim Buffer As New System.IO.StringReader(BBox.Text)
        Dim Line As String = Buffer.ReadLine()
        Do Until Line Is Nothing
          Printer.Print(Line)
          Printer.Print("")
          Line = Buffer.ReadLine()
        Loop
        Printer.EndDoc()
    End Select
  End Sub

  Private Sub HScroll_ScrollChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.ScrollChangedEventArgs) Handles HScroll.ScrollChanged
    BScroll.ScrollToVerticalOffset(HScroll.VerticalOffset)
    If HScroll.ScrollableWidth <> 0 Then
      BScroll.ScrollToHorizontalOffset(BScroll.ScrollableWidth * HScroll.HorizontalOffset / HScroll.ScrollableWidth)
    End If
  End Sub

  Private Sub BreakButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles BreakButton.MouseUp
    'Note that each Braille page consists of 40 line, and each line consists of 25 Braille character.
    For i As Integer = 1 To 40 - (HBox.LineCount Mod 40)
      HBox.AppendText(vbCrLf)
    Next
    HBox.SelectionStart = HBox.Text.Length
    HBox.ScrollToEnd()
  End Sub
End Class
